<template>
	<base-alike-popup :show="loginPopupShow" mode="bottom" round="10" @close='hideTap'>
		<view class="login-wrap textAlign">
			<view class="title f20 color33 fw700">手机号快捷登录</view>
			<view class="desc f12 color99">登录后解锁更多内容</view>
			<view class="btn">
				<button @getphonenumber='getPhoneNumber' open-type="getPhoneNumber"  class="wx-login-btn checkedBackColor fw700 colorff" hover-class='btnHover'>授权登录</button>
				<view @click="toCardLogin" class="card-login-btn checkedColor" hover-class="btnHover">会员卡登录</view>
			</view>
		</view>
	</base-alike-popup>	
</template>

<script>
	import { checkLogin } from '../../utils/util'
	import api from '@/api/userInfo.js'
	import store from '@/store/index.js'
	let app = getApp()
	export default {
		props: { 
			
		},
		data() {
			return {
				loginKey: ''
			}
		},
		computed: {
			loginPopupShow() {
				return this.$store.state.loginPopupShow
			},
		},
		mounted() {
			this.getSessionKey()
		},
		methods: { 
			getSessionKey() {
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {
						let res = await api.getJsCode2Session(loginRes.code)
						this.loginKey = res.result
						uni.setStorageSync('loginKey', this.loginKey)
					}
				});
			},
			hideTap(){
				this.$store.commit('UPDATE_LOGIN_POPUP', false)
			},
			// 获取手机号
			async getPhoneNumber(e) {
				if (e.detail.encryptedData && e.detail.iv) {
					let data = {
						iv: e.detail.iv,
						sessionKey: this.loginKey.session_key,
						encryptedData: e.detail.encryptedData
					}
					let phonenumberRes = await api.decryptMobile(data)
					if (phonenumberRes.result) {
						uni.showLoading({title: '加载中'})
						let data = {
							"validateType": 2,
							"mobile": phonenumberRes.result,
							"password": "123456",
							"openId": this.loginKey.openid,
							"socialLoginType": 6,
						}
						const validateRes = await api.validate(data);
						uni.setStorageSync('openid', this.loginKey.openid)
						switch (validateRes.result.loginStatus) {
							case 200:
								// 登录成功
								store.commit('SET_TOKEN', validateRes.result.accessToken)
								this.getUserBrief()
								break;
							case 600:
								// 用户未注册
								this.validateTel(phonenumberRes.result)
								break;
							default:
								this.$toast(validateRes.result.message)
						}
					} else {
						this.$toast('获取手机号失败')
						this.getSessionKey()
					}
				} else {
					this.$toast('获取手机号失败')
					this.getSessionKey()
				}
				uni.hideLoading()
				this.hideTap()
			},
			// 验证手机号
			async validateTel(mobile) {
				const res = await api.validateTel({mobile})
				if (res.isSuccess) {
					if (res.result.isFreeze) {
						this.$toast('用户已冻结')
						return
					}
					//没有绑定过用户
					if (!res.result.isExist) {
						this.register(mobile, this.loginKey.openid)
					} else {
						this.bindUserInfo(mobile, this.loginKey.openid)
					}
				} else {
					this.$toast(res.message)
				}
			},
			// 绑定用户
			async bindUserInfo(mobile, openid) {
				const data = {
					accountBindType: 2, //1=绑定会员卡,2=绑定第三方社交平台
					mobile,
					openId: openid,
					socialLoginType: 6
				}
				const res = await api.bindUserInfo(data);
				if (res.isSuccess && res.result.accessToken) {
					store.commit('SET_TOKEN', res.result.accessToken)
					this.getUserBrief()
				} else {
					this.$toast(res.result.message)
				}
			},
			// 用户注册
			async register(mobile, openid) {
				let data = {
					"registrationType": 1,
					"mobile": mobile,
					"password": "12345678",
					"openId": openid,
					"socialLoginType": 6,
					"accountType": 1,
					"appChannel": 9
				}
				let registerRes = await api.register(data)
				if (registerRes.isSuccess && registerRes.result.accessToken) {
					store.commit('SET_TOKEN', registerRes.result.accessToken)
					this.getUserBrief()
				} else {
					this.$toast(registerRes.message)
				}
			},
			// 获取用户信息
			async getUserBrief() {
				let userInfoRes = await api.getUserBrief()
				uni.hideLoading()
				if (userInfoRes.isSuccess) {
					let data = uni.getStorageSync('userInfoStorage')
					if(data.ageStage&&!userInfoRes.result.ageStage){
						await api.updateUserInfo(data)
						// 岛屿
						uni.redirectTo({
							url: '/pages/soul-island/pages/index/index'
						})
					}else{
						if(app.globalData.shareUrl){
							uni.reLaunch({
								url:app.globalData.shareUrl
							})
							app.globalData.shareUrl = ""
						}else{
							app.openToNextPage()
						}	
					}
					
				} else {
					this.$toast(userInfoRes.message)
				}
			},
			
			toCardLogin() {
				uni.navigateTo({
					url: '/packages/login/pages/index?type=cardLogin'
				})
				this.hideTap()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.login-wrap {
		padding: 80rpx 64rpx 0;
		padding-bottom: calc(100rpx + env(safe-area-inset-bottom));
		z-index: 6;
		border-radius: 20rpx;
		line-height: 1;
		.title {
			
		}
		.desc {
			margin-top: 20rpx;
		}
		.btn {
			margin-top: 64rpx;
			.wx-login-btn {
				width: 622rpx;
				line-height: 88rpx;
				border-radius: 200rpx;
			}
			.card-login-btn {
				margin-top: 48rpx;
			}
		}
	}
</style>
